import json

from get_companies import get_companies_hrefs
from get_info import get_info
from config import countries, places
import pandas as pd


def get_url():
    urls_list = []
    god = True
    while god:
        for country in countries:
            for place in places:
                try:
                    with open(f'{country}_{place}_hrefs.json') as file:
                        file = json.load(file)
                        string_urls = file[country][place]
                        urls = string_urls.replace('{', '').replace("'",'').replace('}','').split(',')
                        for url in urls:
                            urls_list.append(urls)
                            print(urls_list)
                            return urls_list

                except Exception as ex:
                    continue
                finally:
                    god = False


list_for_uls = []


def main():
    for country in countries:
        for place in places:
            while True:
                get_companies_hrefs(country=country, place=place)
                break

    file = get_url()
    for urls in file:
        for url in urls:
            list_for_uls.append(url)

    count_urls = len(list_for_uls)

    con = get_info(urls=s, uc=2)
    print('SCRIPT FINISHED SUCCESSFUL')
    df = pd.DataFrame(con)
    df.to_excel('data.xlsx')


if __name__ == '__main__':
    main()

